package com.ruoyi.procurementmanagement.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.procurementmanagement.domain.*;
import com.ruoyi.procurementmanagement.mapper.ErpGoodsLabelRelationMapper;
import com.ruoyi.procurementmanagement.service.IEsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.procurementmanagement.mapper.ErpGoodsMapper;
import com.ruoyi.procurementmanagement.service.IErpGoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
public class ErpGoodsServiceImpl implements IErpGoodsService
{
    @Autowired
    private ErpGoodsMapper erpGoodsMapper;

    @Autowired
    private ErpGoodsLabelRelationMapper erpGoodsLabelRelationMapper;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public ErpGoods selectErpGoodsById(Long id)
    {
        return erpGoodsMapper.selectErpGoodsById(id);
    }

    /**
     * 查询商品列表
     *
     * @param erpGoods 商品
     * @return 商品
     */
    @Override
    public List<ErpGoods> selectErpGoodsList(ErpGoods erpGoods)
    {
        return erpGoodsMapper.selectErpGoodsList(erpGoods);
    }

    /**
     * 新增商品
     *
     * @param erpGoods 商品
     * @return 结果
     */
    @Override
    @Transactional
    public ErpGoods insertErpGoods(ErpGoods erpGoods) {
        try {
            // 1. 设置创建时间
            erpGoods.setCreateTime(DateUtils.getNowDate());

            // 2. 执行插入
            int affectedRows = erpGoodsMapper.insertErpGoods(erpGoods);
            if (affectedRows == 0) {
                throw new RuntimeException("插入商品失败，影响行数为0");
            }

            // 3. 确保获取ID（双重保障）
            if (erpGoods.getId() == null) {
                Long id = erpGoodsMapper.selectIdBySn(erpGoods.getSn());
                if (id == null) {
                    throw new RuntimeException("插入后无法获取商品ID");
                }
                erpGoods.setId(id);
            }

            return erpGoods;
        } catch (Exception e) {
            throw new RuntimeException("商品保存失败: " + e.getMessage());
        }
    }

    /**
     * 修改商品
     *
     * @param erpGoods 商品
     * @return 结果
     */
    @Override
    public int updateErpGoods(ErpGoods erpGoods)
    {
        erpGoods.setUpdateTime(DateUtils.getNowDate());
        return erpGoodsMapper.updateErpGoods(erpGoods);
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteErpGoodsByIds(Long[] ids) {
        // 先批量删除标签关联关系
        for (Long id : ids) {
            erpGoodsLabelRelationMapper.deleteByGoodsId(id);
        }
        // 再批量删除商品
        return erpGoodsMapper.deleteErpGoodsByIds(ids);
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteErpGoodsById(Long id) {
        // 先删除标签关联关系
        erpGoodsLabelRelationMapper.deleteByGoodsId(id);
        // 再删除商品
        return erpGoodsMapper.deleteErpGoodsById(id);
    }

    @Override
    public List<ErpProductStock> getProductStockByGoodsId(Long id) {
        return erpGoodsMapper.selectErpProductStockByGoodsId(id);
    }

    @Override
    public List<EsSpecValues> getSpecValuesByGoodsId(Long id) {
        return erpGoodsMapper.selectEsSpecValuesByGoodsId(id);
    }

    @Override
    public List<EsCategorySpec> getCategorySpecByCategoryId(Long categoryId) {
        return erpGoodsMapper.selectEsCategorySpecByCategoryId(categoryId);
    }

    @Override
    public EsCategory getCategoryByCategoryId(Long categoryId) {
        return erpGoodsMapper.selectEsCategoryByCategoryId(categoryId);
    }

    @Autowired
    private IEsCategoryService categoryService;

    @Override
    public List<ErpGoods> selectErpGoodsListByCategory(Long categoryId) {
        // 获取当前分类及其所有子分类ID
        List<Long> categoryIds = categoryService.getAllChildCategoryIds(categoryId);

        ErpGoods query = new ErpGoods();
        query.setCategoryIds(categoryIds);
        return erpGoodsMapper.selectErpGoodsList(query);
    }

    @Override
    public List<ErpGoods> selectErpGoodsListByCategoryIds(List<Long> categoryIds) {
        return erpGoodsMapper.selectErpGoodsListByCategoryIds(categoryIds);
    }

    @Override
    public int countByDate(Date date) {
        return erpGoodsMapper.countByDate(date);
    }

    @Override
    public int getTodayGoodsCount() {
        return erpGoodsMapper.countByDate(new Date());
    }
}
